package com.metaera.app.controller.presenter;

import com.metaera.app.controller.core.RequestPresenter;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.DataRepository;
import com.metaera.app.data.model.UserDetailsResponse;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.data.rx.ResponseObserver;
import com.metaera.app.data.rx.SchedulersTransformer;

public class LoginPresenter extends RequestPresenter<INetworkRequestView<UserDetailsResponse>> {

    public void password(CharSequence phoneNumber, CharSequence password) {

        DataRepository.getInstance()
                .login(
                        ApiParam.LOGIN_PASSWORD_TYPE,
                        null,
                        phoneNumber.toString().replace(" ", ""),
                        password.toString(),
                        null
                )
                .compose(SchedulersTransformer.transformer())
                .subscribe(new ResponseObserver<>(
                                this,
                                mView,
                                response -> mView.successful(response.getData())
                        )
                );
    }


    public void ver(CharSequence phoneNumber, CharSequence code, CharSequence phoneCode) {
        DataRepository.getInstance()
                .login(
                        ApiParam.LOGIN_CODE_TYPE,
                        code.toString(),
                        phoneNumber.toString().replace(" ", ""),
                        null,
                        phoneCode.toString().replace("+", "")
                )
                .compose(SchedulersTransformer.transformer())
                .subscribe(new ResponseObserver<>(
                                this,
                                mView,
                                response -> mView.successful(response.getData())
                        )
                );
    }


    public void wx(String code){
        DataRepository.getInstance()
                .weChatLogin(code)
                .compose(SchedulersTransformer.transformer())
                .subscribe(new ResponseObserver<>(
                                this,
                                mView,
                                response -> mView.successful(response.getData())
                        )
                );
    }

    public void bind(CharSequence phoneNumber, CharSequence code, CharSequence phoneCode, String token){
        DataRepository.getInstance()
                .bindMobile(
                        code.toString(),
                        phoneNumber.toString().replace(" ", ""),
                        phoneCode.toString().replace("+", ""),
                        token)
                .compose(SchedulersTransformer.transformer())
                .subscribe(new ResponseObserver<>(
                                this,
                                mView,
                                response -> mView.successful(response.getData())
                        )
                );
    }


    public void logOff(){
        DataRepository.getInstance()
                .logOff()
                .compose(SchedulersTransformer.transformer())
                .subscribe(new ResponseObserver<>(
                                this,
                                mView,
                                response -> mView.successful(null)
                        )
                );
    }

}
